package com.neuedu.web.servlet.user; /**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-10-19 9:04
 * 描述 : ${dc}
 */

import com.neuedu.web.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "UserUpdateServlet", value = "/userUpdate")
public class UserUpdateServlet extends HttpServlet {
    UserDao userDao = new UserDao();



    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //使用requests接受参数

        //getParameter 返回字符串形式的参数

        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String displayname = request.getParameter("displayname");
        String birthday = request.getParameter("birthday");
        String gender = request.getParameter("gender");
        String email = request.getParameter("email");
        String telephone = request.getParameter("telephone");
        String lastLogintime = request.getParameter("lastLogintime");

        //接收到的参数在控制台中输出
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println("displayname = " + displayname);
        System.out.println("birthday = " + birthday); //yyyy-MM-dd
        System.out.println("gender = " + gender);
        System.out.println("email = " + email);
        System.out.println("telephone = " + telephone);
        System.out.println("lastLogintime = " + lastLogintime);

        //保存到数据库中
        Date dataBirthDay = null;
        Date dataLastLogin = null;
        try {
            dataBirthDay = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            dataLastLogin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(lastLogintime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String sql = "UPDATE  user SET `username` = ?, `displayname` = ?, " +
                "`password` = ?, `birthday` = ?, `gender` = ?,  `telephone` = ?,  `email` = ?, `last_logintime` = ? where id = ? ";
        try {
            int count = userDao.update(sql ,
                    username,
                    displayname,
                    password,
                    dataBirthDay,
                    gender,
                    telephone,
                    email,
                    dataLastLogin,
                    id
                    );

            System.out.println("count = " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        //告诉浏览器 重新请求一次 userlist

        // response.sendRedirect("http://www.baidu.com"); //相对路径
        // 获取上下文
        String contextPath  = request.getContextPath();//上下文
        response.sendRedirect(contextPath+"/userlist"); //相对路径

    }
}
